<template>
  <a-modal
    class="fillmaskModal"
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top: 5%; height: 85%; overflow-y: hidden"
  >

    <div class="info">
      <p>排放设施：{{ model.facilitiesName }}</p>
      <p>排放源：{{ model.boundaryName }}</p>
      <p>所属位置：{{ model.location }}</p>
      <p>排放过程：{{ model.industryName }}-{{ model.boundaryCategoryName }}</p>
    </div>
    <a-spin :spinning="confirmLoading">
      <!--     :rules="validatorRules" -->
      <a-form-model
        ref="form"
        v-bind="layout"
        :model="model"
    
      >
      <!--     prop="consume" -->
        <a-form-model-item
          :label="item.fieldName"
          required
       
          v-for="(item, index) in model.fieldList"
          :key="index"
        >
          <a-input v-model="item.defaultValue" />
        </a-form-model-item>


        <a-form-model-item label="描述" prop="description">
          <a-textarea
            :rows="5"
            v-model="model.description"
            placeholder="请输入角色描述"
          />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import {queryFillingTask, addRole, editRole, duplicateCheck,updateTaskBoundary } from "@/api/api";
export default {
  name: "RoleModal",
  data() {
    return {
      normFile: null,
      title: "操作",
      visible: false,
      roleDisabled: false,
      model: {},
      layout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 12 },
      },
      confirmLoading: false,
      validatorRules: {
        consume: [{ required: true, message: "请填写消耗量!" }],
        roleCode: [
          { required: true, message: "请输入角色名称!" },
          { min: 0, max: 64, message: "长度不超过 64 个字符", trigger: "blur" },
          { validator: this.validateRoleCode },
        ],
        description: [
          {
            min: 0,
            max: 126,
            message: "长度不超过 126 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add() {
      this.edit(this.modelDefault);
    },
    info(record){
      //   console.log(record)
      queryFillingTask({fillingId:record.fillingId,taskId:record.taskId}).then(res=>{
        //   console.log(res)
        this.model = res.result
        this.visible = true
      })
    },
    edit(record) {
      //   console.log(record)
      this.model = Object.assign({}, record);
      // //   console.log(   this.model)
      // let param = {
      //   boundaryId:this.model.boundaryId,
      //   facilitiesId:this.model.facilitiesId,
      //   fillingStatus:this.model.fillingStatus,
      //   taskId:this.model.taskId
      // }
      // queryFillingParamInfo(param).then(res =>{
      //   //   console.log(res)
      // })
      this.visible = true;
      //编辑页面禁止修改角色编码
      if (this.model.id) {
        this.roleDisabled = true;
      } else {
        this.roleDisabled = false;
      }
    },
    close() {
      this.$refs.form.clearValidate();
      this.$emit("close");
      this.visible = false;
    },
    handleOk() {
         const that = this;
            //   console.log(this.model)
        
            var param = {
              description:'',
              filePath:'',
              id:this.model.fillingId,
              jsonInfo:JSON.stringify(this.model.fieldList)
            }
            // updateTaskBoundary(param).then(res =>{
            //   //   console.log(res)
            //     if (res.success) {
            //     that.$message.success(res.message);
            //     that.$emit("ok");
            //      this.close();
            //   } else {
            //     that.$message.warning(res.message);
            //      this.close();
            //   }
            // }).finally(() => {
            //   that.confirmLoading = false;
            //   that.close();
            // });

   
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true;
          let obj;
           obj =   updateTaskBoundary(param)
          // if (!this.model.id) {
          //  obj =   updateTaskBoundary(this.model)
          //   // obj = addRole(this.model);
          // } else {
          //   // obj = editRole(this.model);
          // }
    
          obj
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit("ok");
              } else {
                that.$message.warning(res.message);
              }
            })
            .finally(() => {
              that.confirmLoading = false;
              that.close();
            });
        } else {
          return false;
        }
      });
    },
    handleCancel() {
      this.close();
    },
    validateRoleCode(rule, value, callback) {
      if (/[\u4E00-\u9FA5]/g.test(value)) {
        callback("角色编码不可输入汉字!");
      } else {
        let params = {
          tableName: "sys_role",
          fieldName: "role_code",
          fieldVal: value,
          dataId: this.model.id,
        };
        duplicateCheck(params).then((res) => {
          if (res.success) {
            callback();
          } else {
            callback(res.message);
          }
        });
      }
    },
  },
};
</script>

<style lang="scss" >
@import "@/assets/css/newrModule.scss";
.fillmaskModal .ant-modal-body {
  padding-top: 0px !important;
  padding-left: 0;
  padding-right: 0;
}
.fillmaskModal .ant-spin-nested-loading {
  padding: 12px 24px !important;
}
.fillmaskModal .ant-select-selection-selected-value {
  font-size: 12px;
}
.fillmaskModal .info {
  background: #ebf5fe;
  background-image: linear-gradient(
    rgb(209, 231, 253),
    rgba(245, 249, 253, 0.644)
  );
  padding: 12px 24px !important;
  margin-bottom: 10px;
  //   border-bottom: 1px dashed rgb(241, 241, 241);
  p {
    font-size: 14px;
    font-weight: 600;
    margin: 0;
    margin-bottom: 4px;
  }
}
</style>